<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class NewController extends Controller
{
    //新闻列表
    public function lists(){
    	$where[] = ['menu_article_id','=','1'];
    	$articles = Db::table('article')->where($where)->orderBy('order','DESC')->paginate(15);

    	return view('news_lists',['articles'=>$articles]);
    }

    //添加新闻
    public function add_article(Request $request){

    	return view('news_add');
    }

    //添加新闻动作
    public function add_article_do(Request $request){
    	$input = $request->input();
    	$input['time'] = time();
    	$input['menu_article_id'] = '1';

    	if(empty($input['id'])){
    		Db::table('article')->insert($input);
    	}else{
    		Db::table('article')->where('id','=',$input['id'])->update($input);
    	}

    	return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '操作成功！'
        ]);
    }

    //编辑新闻
    public function edit_article(Request $request,$id){
    	$where[] = ['id','=',$id];
    	$articles = Db::table('article')->where($where)->first();

    	return view('news_edit',['articles'=>$articles,'id'=>$id]);
    }

    //删除新闻
    public function del_article(Request $request,$id){
        $where[] = ['id','=',$id];
        Db::table('article')->where($where)->delete();

        return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '操作成功！'
        ]);
    }
}
